﻿
<Assembly: CLSCompliant(True)> 

''' <summary>
''' Generic tools for codecs.
''' </summary>
Public Module BaseXCodec

    ''' <summary>
    ''' Emumeration of Base X Codecs.
    ''' </summary>
    Public Enum BaseXCodec
        ''' <summary>
        ''' Base64 Codec.
        ''' </summary>
        Base64 = 0
        ''' <summary>
        ''' Base64Url Codec.
        ''' </summary>
        Base64Url = 1
        ''' <summary>
        ''' Base32 Codec.
        ''' </summary>
        Base32 = 2
        ''' <summary>
        ''' Base32Hex Codec.
        ''' </summary>
        Base32Hex = 3
        ''' <summary>
        ''' Base16 Codec.
        ''' </summary>
        Base16 = 4
    End Enum

    ''' <summary>
    ''' Converts the given Base X String , encoded with the given Base X Codec, to a Base Y String.
    ''' </summary>
    ''' <param name="BaseXString">Source encoded string.</param>
    ''' <param name="BaseXCodec">BaseX codec of the source string.</param>
    ''' <param name="BaseYCodec">BaseY codec of the result string</param>
    Public Function Convert(ByVal BaseXString As String, ByVal BaseXCodec As BaseXCodec, ByVal BaseYCodec As BaseXCodec) As String
        Dim result As String = ""
        Dim sourceBytes As Byte()
        Select Case BaseXCodec
            Case BaseXCodec.Base64
                sourceBytes = Base64Codec.Decode(BaseXString)
            Case BaseXCodec.Base64Url
                sourceBytes = Base64UrlCodec.Decode(BaseXString)
            Case BaseXCodec.Base32
                sourceBytes = Base32Codec.Decode(BaseXString)
            Case BaseXCodec.Base32Hex
                sourceBytes = Base32HexCodec.Decode(BaseXString)
            Case BaseXCodec.Base16
                sourceBytes = Base16Codec.Decode(BaseXString)
            Case Else
                sourceBytes = New Byte() {}
        End Select
        Select Case BaseYCodec
            Case BaseXCodec.Base64
                result = Base64Codec.Encode(sourceBytes)
            Case BaseXCodec.Base64Url
                result = Base64UrlCodec.Encode(sourceBytes)
            Case BaseXCodec.Base32
                result = Base32Codec.Encode(sourceBytes)
            Case BaseXCodec.Base32Hex
                result = Base32HexCodec.Encode(sourceBytes)
            Case BaseXCodec.Base16
                result = Base16Codec.Encode(sourceBytes)
            Case Else
                result = Nothing
        End Select
        Return result
    End Function

    ''' <summary>
    ''' Checks if the given (bXString) is a valid (bXCodec) encoded string.
    ''' </summary>
    ''' <param name="bXString">The BaseX String to validate.</param>
    ''' <param name="bXCodec">The BaseX Codec to use for validation.</param>
    Public Function IsValid(ByVal bXString As String, ByVal bXCodec As BaseXCodec) As Boolean
        Select Case bXCodec
            Case BaseXCodec.Base64
                Return Base64Codec.IsValid(bXString)
            Case BaseXCodec.Base64Url
                Return Base64UrlCodec.IsValid(bXString)
            Case BaseXCodec.Base32
                Return Base32Codec.IsValid(bXString)
            Case BaseXCodec.Base32Hex
                Return Base32HexCodec.IsValid(bXString)
            Case BaseXCodec.Base16
                Return Base16Codec.IsValid(bXString)
            Case Else
                Return False
        End Select
    End Function

    ''' <summary>
    ''' Encodes the given (sourceString) which have a (sourceEncoding) text encoding, to the given (bXCodec) BaseX string.
    ''' </summary>
    ''' <param name="sourceString">The source string to be encoded.</param>
    ''' <param name="sourceEncoding">The source string text encoding.</param>
    ''' <param name="bXCodec">The Base X codec to convert to.</param>
    Public Function Encode(ByVal sourceString As String, ByVal sourceEncoding As Text.Encoding, ByVal bXCodec As BaseXCodec) As String
        Select Case bXCodec
            Case BaseXCodec.Base64
                Return Base64Codec.Encode(sourceString, sourceEncoding)
            Case BaseXCodec.Base64Url
                Return Base64UrlCodec.Encode(sourceString, sourceEncoding)
            Case BaseXCodec.Base32
                Return Base32Codec.Encode(sourceString, sourceEncoding)
            Case BaseXCodec.Base32Hex
                Return Base32HexCodec.Encode(sourceString, sourceEncoding)
            Case BaseXCodec.Base16
                Return Base16Codec.Encode(sourceString, sourceEncoding)
            Case Else
                Return Nothing
        End Select
    End Function

    ''' <summary>
    ''' Encodes the given (sourceBytes) Bytes array to the given (bXCodec) BaseX string.
    ''' </summary>
    ''' <param name="sourceBytes">The Bytes array to encode.</param>
    ''' <param name="bXCodec">The Base X codec to convert to.</param>
    Public Function Encode(ByVal sourceBytes As Byte(), ByVal bXCodec As BaseXCodec) As String
        Select Case bXCodec
            Case BaseXCodec.Base64
                Return Base64Codec.Encode(sourceBytes)
            Case BaseXCodec.Base64Url
                Return Base64UrlCodec.Encode(sourceBytes)
            Case BaseXCodec.Base32
                Return Base32Codec.Encode(sourceBytes)
            Case BaseXCodec.Base32Hex
                Return Base32HexCodec.Encode(sourceBytes)
            Case BaseXCodec.Base16
                Return Base16Codec.Encode(sourceBytes)
            Case Else
                Return Nothing
        End Select
    End Function

    ''' <summary>
    ''' Decodes the given (bXString) which is encoded as (bXCodec) Base X codec, to a resuting string with the given (resultEncoding) text encoding.
    ''' </summary>
    ''' <param name="bXString">BaseX String to decode.</param>
    ''' <param name="bXCodec">BaseX codec of the bXString</param>
    ''' <param name="resultEncoding">The text encoding to use for the resulting string.</param>
    Public Function Decode(ByVal bXString As String, ByVal bXCodec As BaseXCodec, ByVal resultEncoding As Text.Encoding) As String
        Select Case bXCodec
            Case BaseXCodec.Base64
                Return Base64Codec.Decode(bXString, resultEncoding)
            Case BaseXCodec.Base64Url
                Return Base64UrlCodec.Decode(bXString, resultEncoding)
            Case BaseXCodec.Base32
                Return Base32Codec.Decode(bXString, resultEncoding)
            Case BaseXCodec.Base32Hex
                Return Base32HexCodec.Decode(bXString, resultEncoding)
            Case BaseXCodec.Base16
                Return Base16Codec.Decode(bXString, resultEncoding)
            Case Else
                Return Nothing
        End Select
    End Function

    ''' <summary>
    ''' Decodes the given (bXString) which is encoded as (bXCodec) Base X codec, to a bytes array.
    ''' </summary>
    ''' <param name="bXString">BaseX String to decode.</param>
    ''' <param name="bXCodec">BaseX codec of the bXString</param>
    Public Function Decode(ByVal bXString As String, ByVal bXCodec As BaseXCodec) As Byte()
        Select Case bXCodec
            Case BaseXCodec.Base64
                Return Base64Codec.Decode(bXString)
            Case BaseXCodec.Base64Url
                Return Base64UrlCodec.Decode(bXString)
            Case BaseXCodec.Base32
                Return Base32Codec.Decode(bXString)
            Case BaseXCodec.Base32Hex
                Return Base32HexCodec.Decode(bXString)
            Case BaseXCodec.Base16
                Return Base16Codec.Decode(bXString)
            Case Else
                Return Nothing
        End Select
    End Function

End Module
